<template>
  <div>
    <br>
    <el-form :inline="true">
      <el-form-item label="企业名称">
        <el-input v-model="searchMap.name" placeholder="企业名称"></el-input></el-form-item>
      <el-button type="primary" @click="fetchDate">查询</el-button>
      <el-button type="primary" @click="handleEdit('')">新增</el-button>
    </el-form>

    <el-table
      :data="list"
      border
      style="width: 100%">
      <el-table-column align="center" prop="id" label="ID" width="80"></el-table-column>
      <el-table-column align="center" prop="name" label="企业名称" width="120"></el-table-column>
      <el-table-column align="center" prop="summary" label="企业简介" width="170"></el-table-column>
      <el-table-column align="center" prop="address" label="企业地址" width="250"></el-table-column>
      <el-table-column align="center" prop="labels" label="标签列表" width="120"></el-table-column>

      <!--<el-table-column align="center" prop="ishot" label="是否热门" width="80"></el-table-column>-->
      <el-table-column label="是否热门" width="80">
      <template slot-scope="scope" >
        <el-switch
          v-model="scope.row.ishot"
          active-color="#13ce66"
          inactive-color="#ff4949"
          :active-value="1"
          :inactive-value="0"
        ></el-switch>
      </template>
      </el-table-column>

      <el-table-column align="center" prop="logo" label="LOGO" width="250"></el-table-column>
      <el-table-column align="center" prop="jobcount" label="职位数" width="80"></el-table-column>
      <el-table-column align="center" prop="url" label="URL"></el-table-column>

      <el-table-column
        fixed="right"
        label="操作"
        width="100">
        <template slot-scope="scope">
          <el-button @click="handleEdit(scope.row.id)" type="text" size="small">修改</el-button>
          <el-button @click="handleDelete(scope.row.id)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="fetchDate"
      @current-change="fetchDate"
      :current-page="currentPage"
      :page-sizes="[5,10,20]"
      :page-size="10"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <el-dialog title="编辑" :visible.sync="dialogFormVisible">
      <el-form label-width="80px">
        <el-form-item label="企业名称"><el-input v-model="pojo.name"></el-input></el-form-item>
        <el-form-item label="企业简介">
          <el-input v-model="pojo.summary" type="textarea" :rows="4"></el-input>
        </el-form-item>
        <el-form-item label="企业地址"><el-input v-model="pojo.address"></el-input></el-form-item>
        <el-form-item label="标签列表"><el-input v-model="pojo.labels"></el-input></el-form-item>
        <el-form-item label="是否热门">
          <el-switch active-value="1" inactive-value="0"  v-model="pojo.ishot"></el-switch>
        </el-form-item>
        <el-form-item label="LOGO">
          <el-upload
            class="avatar-uploader"
            action="https://jsonplaceholder.typicode.com/posts/"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload">
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="职位数"><el-input v-model="pojo.jobcount"></el-input></el-form-item>
        <el-form-item label="URL">
          <el-input v-model="pojo.url">
            <template slot="prepend">Http://</template>
          </el-input>
        </el-form-item>

        <el-button type="primary" @click="handleSave()">保存</el-button>
        <el-button @click="dialogFormVisible = false" >关闭</el-button>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import enterpriseApi from '@/api/enterprise'
import city from '../../api/city'
export default {
    data(){
      return{
        list:[],
        total:0,
        currentPage:1,
        pageSize:10,
        searchMap:{},
        id:'',
        pojo:{},
        cityList:[],
        dialogFormVisible: false,
        imageUrl: ''
      }
    },
  created() {
      this.fetchDate()
      city.getList().then(response=>{
        this.cityList=response.data
      })

  },
  methods:{
      fetchDate(){
        enterpriseApi.search(this.currentPage,this.pageSize,this.searchMap).then(response=>{
          this.list=response.data.rows
          this.total=response.data.total
        })
      },
    handleEdit(id){
        this.id=id
      this.dialogFormVisible=true
      if(id!=''){

        enterpriseApi.findById(id).then(response=>{
          this.pojo=response.data
        })
      }else{
        this.pojo={}
      }


    },
    handleSave(){
      enterpriseApi.update(this.id,this.pojp).then(response=>{
        if (response.flag) {
          this.fetchDate()
          this.$message({
            message: response.message,
            type: 'success'
          });
        } else {
          this.$message.error(response.message);
        }
        this.dialogFormVisible=false
      })

    },
    handleDelete(id){
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        enterpriseApi.deleteById(id).then(reponse=>{
          if (reponse.flag){
            this.$message({
              type: 'success',
              message: reponse.message
            });
          }else{
            this.$message({
              type: 'error',
              message: reponse.message
            });

          }


        })

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });




    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
      this.pojo.logo = this.imageUrl
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    }

  }

}


</script>
